package com.ayu.shortlink.settlement.service.handler.strategy;

import com.ayu.shortlink.settlement.common.enums.DiscountTypeEnum;
import com.ayu.shortlink.settlement.service.bases.strategy.AbstractDiscountStrategy;
import com.google.protobuf.ServiceException;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 立减折扣优惠策略
 */
@Component
public class FixedDiscountStrategy implements AbstractDiscountStrategy {

    /**
     * 处理
     * @param totalAmount 原订单金额
     * @param consumeRule 消费规则
     * @return
     */
    @SneakyThrows
    @Override
    public BigDecimal handler(BigDecimal totalAmount, Map consumeRule) {
        // 进行立减处理 需要考虑一个问题 就是如果优惠卷金额大于等于订单金额 还有一个优惠金额是否正常
        BigDecimal couponAmount = (BigDecimal)consumeRule.get("couponAmount");
        if (couponAmount == null || couponAmount.compareTo(BigDecimal.ZERO) < 0){
            throw new ServiceException("优惠券金额有问题");
        }

        if (totalAmount.compareTo(couponAmount) <= 0){
            return BigDecimal.ZERO;
        }
        // 进行立减操作
        return totalAmount.subtract(couponAmount);
    }

    /**
     * 策略标识
     * @return
     */
    @Override
    public Integer mark() {
        return DiscountTypeEnum.FIXED_DISCOUNT.getType();
    }
}
